package com.mcy.springbatch.job;

import com.mcy.springbatch.entity.SysUser;
import com.mcy.springbatch.job.BaseJobConfig;
import com.mcy.springbatch.reader.ReadUserDataStaxEventItemReader;
import com.mcy.springbatch.writer.SysUserFlatFileItemWriter;
import com.mcy.springbatch.writer.SysUserItemWriter;
import com.mcy.springbatch.writer.SysUserStaxEventItemWriter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.json.JsonItemReader;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 对单个文件进行读写，数据格式：json/xml
 */
@Slf4j
@Configuration
public class HReadWriteDataWithFileJobConfig extends BaseJobConfig {

    //读取xml的reader
    @Autowired
    ReadUserDataStaxEventItemReader readUserDataStaxEventItemReader;

    //读取json的reader
    @Autowired
    JsonItemReader userDataJsonItemReader;

    //写json的writer
    @Autowired
    SysUserFlatFileItemWriter sysUserFlatFileItemWriter;

    //写xml的writer
    @Autowired
    SysUserStaxEventItemWriter sysUserStaxEventItemWriter;

    @Autowired
    SysUserItemWriter sysUserItemWriter;

    @Autowired
    private ListItemReader userDataListItemReader;

    @Bean
    public Job readWriteDataWithFileJob() {
        return jobBuilderFactory.get("readWriteDataWithFileJob")
                .incrementer(new RunIdIncrementer())
                .start(userDataXmlReaderStep())
                .build();

    }

    @Bean
    public Step userDataXmlReaderStep() {
        return stepBuilderFactory.get("userDataXmlReaderStep")
                .<SysUser, SysUser>chunk(100)
                .reader(userDataListItemReader)
                .writer(sysUserFlatFileItemWriter)
                .build();

    }
}